//
//  MFMainController.swift
//  weiBoMf
//
//  Created by lcd on 16/4/6.
//  Copyright © 2016年 lcd. All rights reserved.
//

import UIKit

class MFMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        addBtn()
      
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(btn)
    }
    
    // 懒加载创建btn
    private lazy var btn: UIButton = {
    
        let button = UIButton()
        return button
    }()
}

// MARK - 设置界面
extension MFMainController {
    
    
    /// 添加btn
    private func addBtn (){
    
     tabBar.addSubview(btn)
        
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count)
        btn.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "didClickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
}
    
   @objc private func didClickBtn (){
    print("点击了按钮")
    }
    
    
    /// 添加所有控制器
    private func addChildViewControllers() {
        
        tabBar.tintColor = UIColor.orangeColor()
        addChildControllers(MFHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildControllers(MFMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildControllers(MFDiscoverViewController(), title: "搜索", imageName: "tabbar_discover")
        addChildControllers(MFProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    
    ///添加控制器
    private func addChildControllers(viewC: UIViewController, title: String, imageName: String) {
        
        // 设置标题
        viewC.title = title
        
        // 设置图片
        viewC.tabBarItem.image = UIImage(named: imageName)
        
        // 设置导航控制器
        let nav = UINavigationController(rootViewController: viewC)
        
        addChildViewController(nav)
        
    }







}
    
    
